package com.vercon.moensmart.adapter.base

import android.app.Activity
import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import me.jessyan.autosize.AutoSize

abstract class BaseAdapter<T : ViewBinding, T1>(val context: Context?) :
    RecyclerView.Adapter<BaseAdapter<T , T1>.BaseViewHolder>() {

    var mItemListener: BaseItemListener<T1>? = null
    var list: MutableList<T1> = ArrayList()

    fun setData(list: List<T1>?) {
        this.list.clear()
        this.list.addAll(list!!)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
        AutoSize.autoConvertDensityOfGlobal(context as Activity)
        return baseViewHolder(parent)
    }

    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        val t = list[position]
        bindItemView(holder, position, t, list.size)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    abstract fun baseViewHolder(parent: ViewGroup): BaseViewHolder
    abstract fun bindItemView(viewHolder: BaseViewHolder?, position: Int, t: T1, total: Int)
    fun setItemOnClickListener(baseItemListener:  BaseItemListener<T1>) {
        mItemListener = baseItemListener
    }


    open inner class BaseViewHolder(dataBinding: T): RecyclerView.ViewHolder(dataBinding.root){
        var mBinding = dataBinding
    }

    interface BaseItemListener<T1> {
        fun onItemClickListener(v: View?, data: T1, position: Int)
    }
}